const requireEx=require('yi-require');
const lookup = require('yi-lookup');

const beforeSend = function (req,body,callback) {
  let componentList = (body.data instanceof Array)?body.data:body.data.items;
  let lookupList = [];
  lookupList.push({
    from:"cardKind",
    localField:"cardKindID",
    foreignField:"_id",
    project:{createTime:0,lastModifyDate:0,pwd:0}
  }) ;
  lookup.lookup(req.mongo,componentList,lookupList,(e,r)=>{
    // callback(null,body);
    lookupList = [];
    lookupList.push({
      from:"comment",
      localField:"commentID",
      foreignField:"_id",
      project:{createTime:0,lastModifyDate:0,pwd:0}
    }) ;
    lookup.lookupArray(req.mongo,componentList,lookupList,(e,r)=>{
      callback(null,body);
    });
  });
};

const middleware = (req, res, next) => {
  // let doc = req.query;
  //
  // if(doc.filter && doc.filter.keywords){
  //   doc.filter["$or"] = [
  //     {"title":{"$regex":doc.filter.keywords}},
  //     {"details":{"$regex":doc.filter.keywords}}
  //   ];
  //   delete doc.filter.keywords;
  // }
  console.log('$$$$$$$$$$$$$$$$$$$$$$$$$$',req.query);
  req.beforeSend = beforeSend;
  next();
};

module.exports = {
  description:'获取卡片',
  middleware: middleware,
  handler: requireEx('yi-restful').r('card')
};
